var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageSize:6,
    pageIndex: 1,
    scrollTop: 0,
    scrollHeight:0,
    shopList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    visible:false,
    itemData:[],
    currentTab:0,
    health:0,
    shop_type:0,
    deviceType:0,
    keyword:'',
    position:1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.health = parseInt(options.state);
    if(options.type){
      this.data.shop_type = parseInt(options.type);
    }
    if(options.oil){
      this.data.deviceType = parseInt(options.oil);
    }
    if(options.position){
      this.data.position = parseInt(options.position);
    }
    
    this.setData({
      scrollHeight: app.globalData.windowHeight + 4,
    })

    this.onHttpInit();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    // this.data.pageIndex = 1;
    // this.setData({
    //   loading: false,
    //   netWork: false,
    //   isLoading: true,
    //   hasData:false,
    //   shopList:[]
    // });
  },
  onChangeKey(e){
    console.log('onChangeKey', e)
    this.data.keyword = e.detail;
  },
  onSearch(e){
    this.data.pageIndex = 1;
    this.setData({
      isLoading: true,
      shopList:[],
    })
    this.onHttpInit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/apply/shop/device/record//my/shop/list', {
      shop_type: that.data.shop_type,
      device_type: that.data.deviceType,
      pageSize:that.data.pageSize,
      pageIndex:that.data.pageIndex,
      keyword: that.data.keyword,
      position: that.data.position,
    }, function (res) {
      console.log("传感器设备列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var shops = res.data.data.shops;
          console.log('shops',shops)
          for (var index in shops) {
            that.data.shopList.splice(that.data.shopList.length, 0, shops[index]);
          }
          that.setData({
            hasData: true,
            shopList: that.data.shopList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
        
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    })
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index].imgUrl)
    }
    common_Js.preImg(a, c)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshshopList()
  },
  refreshshopList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      shopList:[]
    })
    this.data.pageIndex = 1;
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
  goDeviceList(res){
    var id = res.currentTarget.dataset.id;
    wx.navigateTo({ url: '/pages/apply/device/index?shopId=' + id +'&shop_type= ' + this.data.shop_type + '&device_type=' + this.data.deviceType+'&position=' + this.data.position })
  }
})